package com.zhuoyue.listener;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.dao.DeviceDao;
import com.zhuoyue.biz.core.api.dao.TblPasswordShareMapper;
import com.zhuoyue.biz.core.api.dao.ZigbeeDevicePropertyMapper;
import com.zhuoyue.biz.core.api.domain.DeviceDO;
import com.zhuoyue.biz.core.api.domain.TblPasswordShare;
import com.zhuoyue.biz.core.api.domain.ZigbeeDeviceProperty;
import com.zhuoyue.biz.core.api.service.DeviceTaskService;
import com.zhuoyue.biz.core.api.service.GatewayRedisService;
import com.zhuoyue.common.mqtt.MqttDataHandle;
import com.zhuoyue.common.mqtt.Topic;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wjming
 * @ClassName: GeneralQueueProcessing
 * @Description:通用队列处理
 * @Date 2021/2/2 20:13
 */
@Component
@RabbitListener(queues = "${topic.rabbit.generalQueueProcessing}")
public class GeneralQueueProcessing {
    @Autowired
    private TblPasswordShareMapper passwordShareMapper;

    @Autowired
    private GatewayRedisService gatewayRedisService;

    @Autowired
    private DeviceTaskService deviceTaskService;
    @Autowired
    private ZigbeeDevicePropertyMapper zigbeeDevicePropertyMapper;

    @Autowired
    private MqttDataHandle mqttDataHandle;

    @Autowired
    private DeviceDao deviceDao;

    /**
     * @Description:数据帧处理通用数据处理
     * @param: getMap
     * @return: void
     * @author:wjming
     * @Date:2021/2/2 20:16
     */
    @RabbitHandler
    public void generalDeal(Map<String, Object> getMap) {
        String type = (String) getMap.get("dealType");
        try {
            switch (type) {
                //德斯慢指纹锁类型
                case "deleteSqlOncePsd":
                    //一次性密码使用后修改数据库信息
                    passwordShareMapper.delete(Wrappers.<TblPasswordShare>lambdaQuery()
                            .eq(TblPasswordShare::getDeviceMac, getMap.get("deviceMac"))
                            .eq(TblPasswordShare::getType, getMap.get("type")));
                    break;

                //指纹联控场景
                case "dealSceneById":
                case "fingerprintLinkageScene":
                    gatewayRedisService.sceneTriggerFrame((String) getMap.get("sceneId"));
                    break;

                //红外学习仪定时任务
                case "setTimedTask":
                    String zigbeeMac = getMap.get("zigbeeMac").toString();
                    int taskIndex = Integer.parseInt(getMap.get("taskIndex").toString());
                    //数据库操作
                    deviceTaskService.addOrUpdateDeviceTask(zigbeeMac, taskIndex);
                    //发送mqtt
                    DeviceDO deviceDO = deviceDao.selectByMac(zigbeeMac);
                    Map<String, Object> rep = new HashMap<>(5);
                    rep.put("type", "timedTask");
                    rep.put("gatewayMac", deviceDO.getGatewayMac());
                    rep.put("zigbeeMac", zigbeeMac);
                    rep.put("code", "00");
                    rep.put("taskIndex", taskIndex);
                    mqttDataHandle.basePush(Topic.Device.TOPIC + deviceDO.getGatewayMac() + Topic.TIMED_TASK, JSONObject.toJSONString(rep));
                    break;
                //继电器模式配置
                case "setRelayMode":
                    String state = (String) getMap.get("controlType");
                    String id = (String) getMap.get("id");
                    ZigbeeDeviceProperty zigbeeDeviceProperty = new ZigbeeDeviceProperty();

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("relay", state);
                    zigbeeDeviceProperty.setProperty(jsonObject.toJSONString());
                    zigbeeDeviceProperty.setId(id);
                    ZigbeeDeviceProperty zigbeeDeviceProperty1 = zigbeeDevicePropertyMapper.selectById(id);
                    if (zigbeeDeviceProperty1 == null) {
                        zigbeeDevicePropertyMapper.insert(zigbeeDeviceProperty);
                    } else {
                        zigbeeDevicePropertyMapper.updateById(zigbeeDeviceProperty);
                    }
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            System.out.println("e = " + e);
        }

    }
}
